package com.neuedu.mp.service.impl;

import com.neuedu.mp.entity.SysMenu;
import com.neuedu.mp.mapper.SysMenuMapper;
import com.neuedu.mp.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.mp.vo.SysMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author 金山老师
 * @since 2023-03-15
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

//    @Autowired
//    SysMenuMapper sysMenuMapper;



    /**
     * 1 所有的菜单， 查询出来，
     *
     * 2 List  将顶层的菜单 查询出来，放到集合中
     *
     *          递归的查找子菜单
     *
     *
     *
     *
     * @return
     */
    @Override
    public List<SysMenuVO> loadTree() {

        List<SysMenu> all = this.list();

        List<SysMenuVO> listRet = new ArrayList();

        for (SysMenu sysMenu : all) {
            if(sysMenu.getParentId() == 0){



                SysMenuVO sysMenuVo = new SysMenuVO();
                sysMenuVo.setMenuId(sysMenu.getMenuId());
                sysMenuVo.setMenuName(sysMenu.getMenuName());
                sysMenuVo.setParentId(sysMenu.getParentId());
                listRet.add(sysMenuVo);

                //子菜单处理下 sysMenuVo  系统管理  id =1
                dealChildren(sysMenuVo , all );

            }
        }


        return listRet;
    }



    public void  dealChildren(SysMenuVO sysMenuVoParent ,   List<SysMenu> all){
        for (SysMenu sysMenu : all) {
            //找到了 sysMenuVo 子菜单了
            if(sysMenu.getParentId() == sysMenuVoParent.getMenuId()){

                SysMenuVO sysMenuVo = new SysMenuVO();

                sysMenuVo.setMenuId(sysMenu.getMenuId());
                sysMenuVo.setMenuName(sysMenu.getMenuName());
                sysMenuVo.setParentId(sysMenu.getParentId());

                sysMenuVoParent.getChildren().add( sysMenuVo  );

                //处理当前子节点的子节点  用户管理
                dealChildren(sysMenuVo ,all);

            }
        }

    }


    /**
     * 查询菜单权限
     * @param userId
     * @return
     */
    @Override
    public List<SysMenu> getUserPerms(Long userId) {

        List<SysMenu> userPerms = getBaseMapper().getUserPerms(userId);

        return userPerms;
    }











}
